var t = getApp(),
  core = t.requirejs("core"),
  e = t.requirejs("check");
Page({
  data: {
    route: "goods_index",
    menu: t.tabBar,
    menu_show: false,
    icons: t.requirejs("icons"),
    page: 1,
    list:[],
    defaults: {
      keywords: "",
      isrecommand: "",
      ishot: "",
      isnew: "",
      cate_id: "",
      order: "",
      by: "desc",
    },
    listmode: "block",//并列展示，为空时列表展示
    fromsearch: false,//控制取消按钮
    isFilterShow: false,//筛选类目
    params:[],
  },
  //底部导航跳转
  menu_url: function (k) {
    core.menu_url(k, 2);
  },
  onLoad: function (o) {

    if (o != null && o != undefined) {
      this.setData({
        tabbar_index: o.tabbar_index ? o.tabbar_index : -1
      });
    }

    core.setting();
    this.setData({
      menu: getApp().tabBar
    })

    if (this.data.tabbar_index >= 0) {
      this.setData({
        showtabbar: true,
      })
    }
    
    var a = this.data.defaults;
    a.keywords=o.key || '';
    a.cate_id = o.cate || '';
  
      this.setData({
        filterBtns: o,
        fromsearch: o.fromsearch || false,
        defaults: a
    });  
    
      this.initCategory();
      this.data.fromsearch || this.getList()
  },
  onShow: function () {

  },
  /**
  * 下拉刷新
  */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      page: 1,
      loaded: false
    })
    this.getList()
    wx.stopPullDownRefresh();
  },
  /**
   *上拉加载
   */
  onReachBottom: function () {
    this.data.loaded || this.getList()
  },

  /**
   *获取一级分类
   * @return array
   */
  initCategory: function () {
    var t = this;
    core.get("goods/GetCate", {}, function (a) {
      console.log(a)
      t.setData({
        category: a.info,
      })
    })
  },

  /**
   *获取商品列表
   * @param  keywords  搜索关键字
   * @param isrecommand 是否推荐
   * @param   ishot  是否热销
   * @param  isnew   是否新品
   * @param  cate_id  类型id
   * @param   order 排序字段
   * @param   by:  倒序/顺序
   * @return array
   */
  getList: function () {
    var t = this;
    t.setData({
      loading: true
    }),
      t.data.defaults.page = t.data.page,
      core.get("goods/GoodsList", t.data.defaults, function (a) {
       if(a.code==0){
        if (a.info.length==0){
          t.setData({
            loading: false
          })
          return false;
        }
        var e = {
          loading: false,
        };
        a.info.length > 0 && (e.page = t.data.page + 1, e.list = t.data.list.concat(a.info), a.info.length < 10 && (e.loaded = true)),
          t.setData(e)
       }else{
         core.alert(a.msg)
       }

      })
 
  },
  
  /**
   *搜索查询
   * @param  keywords  搜索关键字
   * @return array
   */
  bindSearch: function (t) {
    this.setData({
      list: [],   //清空已存在商品列表
      loading: true,
    });
    var a = e.trim(t.detail.value),
      s = this.data.defaults;
      //console.log(a)
    "" != a ? (s.keywords = a, this.setData({
      page: 1,
      defaults: s,
      fromsearch: false
    }), this.getList()) : (s.keywords = "", this.setData({
      page: 1,
      defaults: s,
      fromsearch: false,
    }),  this.getList())
  },
  
  /**
   *提交筛选内容
   */
  bindFilterSubmit: function () {
    var t = this.data.params,
      a = this.data.filterBtns;
    for (var s in a)
      t[s] = a[s];
    e.isEmptyObject(a) && (t = this.data.defaults),
      t.cate = this.data.category_selected,
      this.setData({
        page: 1,
        params: t,
        isFilterShow: false,
        filterBtns: a,
        list: [],
        loading: true
      })
      //console.log(t)
      this.getList()
  },

  /**
   *按综合、热销，价格排序
   */
  bindSort: function (t) {
    var a = core.pdata(t).order,
      e = this.data.defaults;
     // console.log(t)
    if ("" == a) {
      if (e.order == a)
        return;
      e.order = ""
    } else if ("price" == a){
      e.order == a ? "desc" == e.by ? e.by = "asc" : e.by = "desc" : e.by = "asc", e.order = a
    }    
    else if ("sales" == a) {
      if (e.order == a)
        return;
        e.order = "sales",
        e.by = "desc"
    }
    this.setData({
      defaults: e,
      page: 1,
      list: [],
      loading: true,
    }),
   // console.log(e)
      this.getList()
  },
 
  /**
   *筛选中选择商品类别
   */
  bindCategoryEvents: function (t) {
    var a = t.target.dataset.id;
    //console.log(a)
   this.setData({
      "defaults.cate_id": a
    })
  },

 /**
   *提交筛选
   */
  btnFilterBtns: function (t) {
    var a = t.target.dataset.type,
      s = this.data.defaults;
    if (a) {
      //var s = this.data.filterBtns;
      s.hasOwnProperty(a) || (s[a] = ""),
        s[a] ? s[a] = '' : s[a] = 1;
      this.setData({
        defaults: s,
      })
    }
  },
  
  /**
   *隐藏筛选
   */
  bindFilterCancel: function () {
    this.setData({
      isFilterShow: false,
    })
  },
  
  /**
   *展开筛选
   */
  showFilter: function () {
    this.setData({
      isFilterShow: !this.data.isFilterShow
    })
  },

  /**
   *返回
   */
  bindback: function () {
    wx.navigateBack()
  },
})